﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography.X509Certificates;

[CustomEditor(typeof(SpriteAssets))]
public class SpriteAssetsEditor : Editor
{
    private SpriteAssets m_SpriteAssets;
    private Vector2 scrollView;
    private List<string> m_Tags;//当前的所有标签
    private List<int> playIndexs;//当前的所有标签序列动画索引
    private float m_Index;

    private int showIndex;

    private float playSpeed;

    private bool addTag;

    private string addTagName;

    private Dictionary<SpriteInfoGroup, bool> _selectSpriteGroup = new Dictionary<SpriteInfoGroup, bool>();

    bool need = false;
    string filePath;
    public void OnEnable()
    {
        m_SpriteAssets = (SpriteAssets)target;
        playSpeed = 6;
        InitData();
    }

    public void OnDisable()
    {

    }

    public override void OnInspectorGUI()
    {
        //GUI.enabled = false;
        m_SpriteAssets.sourceTexture = EditorGUILayout.ObjectField("", m_SpriteAssets.sourceTexture, typeof(Texture), false, GUILayout.Width(80)) as Texture;
        scrollView = GUILayout.BeginScrollView(scrollView);
        {
            for (int i = 0; i < m_SpriteAssets.groupList.Count; i++)
            {
                GUILayout.BeginHorizontal("HelpBox");
                _selectSpriteGroup[m_SpriteAssets.groupList[i]] = GUILayout.Toggle(_selectSpriteGroup[m_SpriteAssets.groupList[i]], "");
                #region 展开与收缩按钮
                if (GUILayout.Button(m_SpriteAssets.groupList[i].Tag, showIndex == i ? "OL Minus" : "OL Plus"))
                {
                    if (showIndex == i)
                    {
                        showIndex = -1;
                    }
                    else
                    {
                        showIndex = i;
                    }
                }
                #endregion

                GUILayout.BeginHorizontal();
                GUILayout.Label("Size:", GUILayout.Width(40));
                m_SpriteAssets.groupList[i].Size = EditorGUILayout.FloatField("", m_SpriteAssets.groupList[i].Size, GUILayout.Width(40));
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Width:", GUILayout.Width(40));
                m_SpriteAssets.groupList[i].Width = EditorGUILayout.FloatField("", m_SpriteAssets.groupList[i].Width, GUILayout.Width(40));
                GUILayout.EndHorizontal();

                #region 未展开的sprite组，播放序列帧动画（帧数大于1的序列帧动画才播放）
                if (showIndex != i && m_SpriteAssets.groupList[i].ListSpriteInfos.Count > 0)
                {
                    if (playIndexs[i] >= m_SpriteAssets.groupList[i].ListSpriteInfos.Count)
                        playIndexs[i] = 0;

                    GUI.enabled = false;
                    EditorGUILayout.ObjectField("", m_SpriteAssets.groupList[i].ListSpriteInfos[playIndexs[i]].Sprite, typeof(Sprite), false);
                    GUI.enabled = true;
                }
                #endregion
                GUILayout.EndHorizontal();

                #region 展开的sprite组，显示所有sprite属性
                if (showIndex == i)
                {
                    for (int j = 0; j < m_SpriteAssets.groupList[i].ListSpriteInfos.Count; j++)
                    {
                        GUILayout.FlexibleSpace();
                        GUILayout.BeginHorizontal("sprite" + j, "window");
                        m_SpriteAssets.groupList[i].ListSpriteInfos[j].Sprite = EditorGUILayout.ObjectField("", m_SpriteAssets.groupList[i].ListSpriteInfos[j].Sprite, typeof(Sprite), false, GUILayout.Width(80)) as Sprite;

                        GUILayout.FlexibleSpace();

                        GUILayout.BeginVertical();

                        GUI.enabled = false;

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("ID:", GUILayout.Width(50));
                        m_SpriteAssets.groupList[i].ListSpriteInfos[j].Id = EditorGUILayout.IntField(m_SpriteAssets.groupList[i].ListSpriteInfos[j].Id);
                        GUILayout.EndHorizontal();

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Name:", GUILayout.Width(50));
                        m_SpriteAssets.groupList[i].ListSpriteInfos[j].Name = EditorGUILayout.TextField(m_SpriteAssets.groupList[i].ListSpriteInfos[j].Name);
                        GUILayout.EndHorizontal();

                        GUI.enabled = true;

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Tag:", GUILayout.Width(50));
                        if (GUILayout.Button(m_SpriteAssets.groupList[i].ListSpriteInfos[j].Tag, "MiniPopup"))
                        {
                            //GenericMenu gm = new GenericMenu();
                            //for (int n = 0; n < _tags.Count; n++)
                            //{
                            //    int i2 = i;
                            //    int j2 = j;
                            //    int n2 = n;
                            //    gm.AddItem(new GUIContent(_tags[n2]), false,
                            //        delegate ()
                            //        {
                            //        ChangeTag(_tags[n2], SpriteAssets.ListSpriteGroup[i2].ListSpriteInfor[j2]);
                            //        });
                            //}
                            //gm.ShowAsContext();
                        }
                        GUILayout.EndHorizontal();

                        GUI.enabled = false;

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Pivot:", GUILayout.Width(50));
                        EditorGUILayout.Vector2Field("", m_SpriteAssets.groupList[i].ListSpriteInfos[j].Pivot);
                        GUILayout.EndHorizontal();

                        GUILayout.BeginHorizontal();
                        GUILayout.Label("Rect:", GUILayout.Width(50));
                        EditorGUILayout.RectField("", m_SpriteAssets.groupList[i].ListSpriteInfos[j].Rect);
                        GUILayout.EndHorizontal();

                        for (int m = 0; m < m_SpriteAssets.groupList[i].ListSpriteInfos[j].UV.Length; m++)
                        {
                            GUILayout.BeginHorizontal();
                            GUILayout.Label("UV" + m + ":", GUILayout.Width(50));
                            EditorGUILayout.Vector2Field("", m_SpriteAssets.groupList[i].ListSpriteInfos[j].UV[m]);
                            GUILayout.EndHorizontal();
                        }

                        GUI.enabled = true;

                        GUILayout.EndVertical();

                        GUILayout.EndHorizontal();
                    }
                }
                #endregion
            }

        }

        GUILayout.EndScrollView();


        EditorUtility.SetDirty(m_SpriteAssets);

    }

    void InitData()
    {
        m_Tags = new List<string>();
        playIndexs = new List<int>();

        _selectSpriteGroup.Clear();

        for (int i = 0; i < m_SpriteAssets.groupList.Count; i++)
        {
            m_Tags.Add(m_SpriteAssets.groupList[i].Tag);
            playIndexs.Add(0);
            _selectSpriteGroup.Add(m_SpriteAssets.groupList[i], false);
        }

        m_Index = 0;
        showIndex = -1;
        addTag = false;
        addTagName = "";
    }






}
